<template>
  <div class="agriMonitoring">
    <!-- 搜索框 -->
    <div class="searchBox">
      <el-select
        v-model="params.landId"
        popper-class="selectInp"
        :popper-append-to-body="false"
        class="searchInp"
        placeholder="选择地块"
         clearable
        @change="landChange"
      >
        <el-option
          v-for="item in landList"
          :key="item.landId"
          :label="item.landName"
          :value="item.landId"
        >
        </el-option>
      </el-select>
      <el-select
        v-model="params.NS"
        class="searchInp"
        placeholder="农事类型"
        clearable
        @change="NsChange"
      >
        <el-option
          v-for="item in missionTypes"
          :key="item.id"
          :label="item.name"
          :value="item.code"
        >
        </el-option>
      </el-select>
      <el-date-picker
        v-model="dateValue"
        type="daterange"
        range-separator="至"
        value-format="yyyy-MM-dd"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        @change="changeDate"
      >
      </el-date-picker>
      <el-button
        class="searchBtn"
        type="primary"
        icon="el-icon-plus"
        @click="add()"
        >添加农事</el-button
      >
    </div>
    <!-- 列表 -->
    <!-- 农事记录 -->
    <div class="block">
      <div
        class="missionList"
        v-for="item in missions"
        :key="item.index"
        @click="openEdit(item)"
      >
        <div class="missionInfo">
          <div class="missionTime">{{ item.time }}</div>
          <div class="missionInfoType">{{ item.type }}</div>
          <div class="missionUser">操作人：{{ item.participants }}</div>
        </div>

        <div class="missionRemark">{{ item.remark }}</div>
        <!-- 防治 -->
        <div class="farmStockList" v-if="item.code === 'NS_FZ'">
          <div
            class="farmsStock"
            v-for="agriList in item.agriList"
            :key="agriList.id"
          >
            <div class="farmsStockIcon">
              <img src="../../assets/img/huafeiicon.png" alt="" />
            </div>
            <div class="farmsStockName">{{ agriList.name }}</div>
            <div class="farmsStockNum">{{ agriList.num }}公斤</div>
          </div>
        </div>
        <!-- 施肥 -->
        <div class="farmStockList" v-if="item.code === 'NS_SF'">
          <div
            class="farmsStock"
            v-for="agriList in item.agriList"
            :key="agriList.id"
          >
            <div class="farmsStockIcon">
              <img src="../../assets/img/huafeiicon.png" alt="" />
            </div>
            <div class="farmsStockName">{{ agriList.name }}</div>
            <div class="farmsStockNum">{{ agriList.num }}公斤</div>
          </div>
        </div>
        <!-- 播种 -->
        <div class="seed" v-if="item.code === 'NS_BZ'">
          品种：{{ item.seedType }} {{ item.seedInfo }}
          <div class="seedNum" v-if="item.seedNum !== null">
            种子数量：{{ item.seedNum }}公斤
          </div>
          <div class="seedSpacing" v-if="item.seedSpacing !== null">
            种间距：{{ item.seedSpacing }}cm
          </div>
        </div>
        <div
          class="missionImg"
          v-for="(pic, index) in item.picture"
          :key="index"
          @click.stop="pictureBig(pic)"
        >
          <img :src="pic" alt="" />
        </div>
      </div>

      <el-dialog :visible.sync="dialogVisible" width="800" append-to-body>
        <img
          :src="dialogImageUrl"
          style="display: block; max-width: 100%; margin: 0 auto"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getLandDetailApi, delLandInfoApi } from '@/api/agriLand'
import { getSysSonDictListApi } from '@/api/dict'
import { getAgriListByTimeApi } from '@/api/mission'
export default {
  name: 'agriMonitoring',
  data () {
    return {
      landParams: {
        baseId: 1,
        landId: ''
      },
      params: {
        baseId: 1,
        landId: '',
        NS: '',
        startTime: '',
        endTime: ''
      },
      landList: null,
      missionTypes: null,
      landStates: [
        { value: '空闲中', label: 0 },
        { value: '种植中', label: 1 }
      ],
      valueArea: '',
      currentPage: 1,
      dateValue: '',
      tableData: [],
      missions: [],
      dialogImageUrl: '',
      dialogVisible: false,
      landMissionData: []
    }
  },
  mounted () {
    this.landParams.baseId = this.$store.getters.userInfo.baseId
    this.params.baseId = this.$store.getters.userInfo.baseId
    this.getSysSonDictList()
  },

  methods: {
    // 数据处理
    changeData () {
      // console.log(this.landMissionData)
      this.missions = this.landMissionData
      for (let i = 0; i < this.missions.length; i++) {
        if (
          this.missions[i].picture !== '' &&
          this.missions[i].picture !== null &&
          this.missions[i].picture !== undefined
        ) {
          this.missions[i].picture = this.landMissionData[i].picture
            .split(',')
            .map((picture) => picture)
        } else {
          this.missions[i].picture = []
        }
      }
      console.log(this.missions)
    },
    // 查询农事
    async getAgriListByTime (params) {
      if (params.landId === '') {
        this.$message.error('暂无地块')
      } else {
        const res = await getAgriListByTimeApi(params)
        console.log(res)
        this.landMissionData = res.data
        this.changeData()
      }
    },
    openEdit (data) {
      if (data.code === 'NS_BZ') {
        this.$message('请在地块详情中查看播种信息')
      } else if (data.code !== 'NS_BZ') {
        this.$store.dispatch('mission/updatemissionDetail', data)
        // console.log(this.missionDetail)
        this.$router.push({
          path: '/shuzhongtian/agriMissionAdd',
          query: {
            ...this.$route.query
          }
        })
      }
      // console.log(data)
      // this.$store.dispatch('mission/updatemissionDetail', data)
      // // console.log(this.missionDetail)
      // this.$router.push({
      //   path: '/shuzhongtian/agriMissionAdd',
      //   query: {
      //     ...this.params, pageType: '1'
      //   }
      // })
    },
    // 照片放大
    pictureBig (pic) {
      this.dialogImageUrl = pic
      this.dialogVisible = true
    },
    // 添加农事
    addLand () {
      this.$router.push({
        path: '/shuzhongtian/agriAreaAdd'
      })
    },
    // 字典
    async getSysSonDictList () {
      // 地块
      const res = await getLandDetailApi(this.landParams)
      this.landList = res.data
      // 地块类型
      const res2 = await getSysSonDictListApi('NS')
      // console.log(res2.data)
      this.missionTypes = res2.data
      if (this.landList.length !== 0) {
        this.params.landId = this.landList[0].landId
        this.getAgriListByTime(this.params)
      } else {
        this.$message.error('暂无地块')
      }
    },
    // 删除按钮
    async delBtn (id) {
      const res = await delLandInfoApi(id)
      if (res.code === 200) {
        this.$message.success('删除成功')
        this.getLandInfoPage(this.params)
      } else {
        this.$message.error('删除失败')
      }
    },
    changeDate (e) {
      console.log(e)
      if (e === null) {
        this.params.startTime = ''
        this.params.endTime = ''
        this.getAgriListByTime(this.params)
      } else {
        this.params.startTime = e[0]
        this.params.endTime = e[1]
        this.getAgriListByTime(this.params)
      }
    },
    NsChange () {
      this.getAgriListByTime(this.params)
    },
    landChange () {
      this.getAgriListByTime(this.params)
    },
    // 查询
    add () {
      this.$router.push({
        path: '/shuzhongtian/agriMissionAdd',
        query: {
          ...this.params
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.agriMonitoring {
  width: 100%;
  height: 100%;
  position: relative;
  // background-color: #fff;
  .searchBox {
    font-weight: 600;
    margin-bottom: 1.5625vw;
    width: 19.25rem;
    height: 6.25vw * 0.834;
    line-height: 6.25vw * 0.834;
    background: #ffffff;
    border-radius: 20px 20px 20px 20px;
    padding: 0 0.37rem;
    ::v-deep .el-select > .el-input {
      height: 3.5vw;
    }
    .searchInp {
      width: 16.4708vw;
      height: 0.75rem * 0.834;
      margin-right: 1vw;
    }
    .searchBtn {
      background-color: #19b560;
      border-radius: 12px 12px 12px 12px;
      font-weight: 600;
      width: 12.5vw;
      height: 3.125vw * 0.834;
      margin-left: 4.5833vw;
    }
    // .searchBtn:hover {
    //   background-color: #80efb1;
    // }
  }
  .tableList {
    width: 80.2083vw;
    height: 35.4167vw * 0.834;
    border-radius: 20px;
    background: #fff;
    padding: 1.0417vw;
    .addBtn {
      margin-bottom: 1.5625vw * 0.834;
    }
    .list {
      text-align: center;
    }
  }
  .pagination {
    margin-top: 1.5625vw * 0.834;
    text-align: center;
  }
}
// 任务列表
.block {
  height: 34vw;
}
.missionList {
  width: 79.0833vw;
  background: #fff;
  border-radius: 12px 12px 12px 12px;
  padding: 1.0417vw * 0.834 1.0417vw;
  margin-bottom: 1.5vw;
}
.missionInfo {
  display: flex;
  .missionTime {
    width: 8.3333vw;
    height: 1.5625vw * 0.834;
    background: #19b560;
    border-radius: 8px 8px 8px 8px;
    margin-right: 20px;
    text-align: center;
    line-height: 1.5625vw * 0.834;
    font-weight: 500;
    font-size: 1.0417vw;
    color: #fff;
  }
  .missionInfoType {
    // width: 8.1667vw;
    padding: 0 1vw;
    height: 1.5625vw * 0.834;
    background: #19b560;
    border-radius: 8px 8px 8px 8px;
    margin-right: 20px;
    line-height: 1.5625vw * 0.834;
    text-align: center;
    font-weight: 500;
    font-size: 1.0417vw;
    color: #fff;
  }
  .missionUser {
    width: 10.1667vw;
    height: 1.5625vw * 0.834;
    line-height: 1.5625vw * 0.834;
    font-weight: 500;
    font-size: 1.0417vw;
  }
}
.missionRemark {
  font-weight: 400;
  font-size: 1.0417vw;
  color: #666666;
  margin: 1.0417vw * 0.834 0 0.5208vw * 0.834;
}
.farmStockList {
  width: 70vw;
  display: flex;
}
.farmsStock {
  width: 18.75vw;
  height: 3.2813vw * 0.834;
  margin-right: 1.0417vw;
  background: #e4e7e9;
  border-radius: 12px 12px 12px 12px;
  display: flex;
  line-height: 3.2813vw * 0.834;
  font-size: 0.9375vw;
  .farmsStockIcon {
    width: 1.6667vw;
    height: 1.6667vw;
    margin: auto 1.0417vw;
  }
  .farmsStockName {
    margin: 0 1.5625vw 0 0;
    color: #647f93;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
  }
  .farmsStockNum {
    color: #647f93;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
  }
}
.missionImg {
  margin-top: 0.5208vw * 0.834;
  width: 5.5vw;
  height: 5.5vw;
  background: #dcdedf;
  border-radius: 12px 12px 12px 12px;
  display: inline-block;
  margin-right: 0.8208vw;
  img {
    width: 5.5vw;
    height: 5.5vw;
  }
}
@media screen and (max-width: 1450px) {
  .agriMonitoring {
  width: 100%;
  height: 100%;
  position: relative;
  // background-color: #fff;
  .searchBox {
    font-weight: 600;
    margin-bottom: 1.5625vw *0.77;
    width: 19.25rem;
    height: 6.25vw * 0.77;
    line-height: 6.25vw * 0.77;
    background: #ffffff;
    border-radius: 15px 15px 15px 15px;
    padding: 0 0.37rem;
    ::v-deep .el-select > .el-input {
      height: 3.5vw;
    }
    .searchInp {
      width: 16.4708vw;
      height: 0.75rem * 0.77;
      margin-right: 1vw;
    }
    .searchBtn {
      background-color: #19b560;
      border-radius: 12px 12px 12px 12px;
      font-weight: 600;
      width: 12.5vw;
      height: 3.125vw * 0.77;
      margin-left: 4.5833vw *0.77;
    }
  }
  .tableList {
    width: 80.2083vw;
    height: 35.4167vw * 0.77;
    border-radius: 20px;
    background: #fff;
    padding: 1.0417vw;
    .addBtn {
      margin-bottom: 1.5625vw * 0.77;
    }
    .list {
      text-align: center;
    }
  }
  .pagination {
    margin-top: 1.5625vw * 0.77;
    text-align: center;
  }
}
// 任务列表
.block {
  height: 34vw *0.77;
  overflow-y: auto;
}
.missionList {
  width: 79.0833vw;
  background: #fff;
  border-radius: 12px 12px 12px 12px;
  padding: 1.0417vw * 0.77 1.0417vw;
  margin-bottom: 1.5vw;
}
.missionInfo {
  display: flex;
  .missionTime {
    width: 8.3333vw;
    height: 1.5625vw * 0.77;
    background: #19b560;
    border-radius: 8px 8px 8px 8px;
    margin-right: 20px;
    text-align: center;
    line-height: 1.5625vw * 0.77;
    font-weight: 500;
    font-size: 1.0417vw;
    color: #fff;
  }
  .missionInfoType {
    // width: 8.1667vw;
    padding: 0 1vw;
    height: 1.5625vw * 0.77;
    background: #19b560;
    border-radius: 8px 8px 8px 8px;
    margin-right: 20px;
    line-height: 1.5625vw * 0.77;
    text-align: center;
    font-weight: 500;
    font-size: 1.0417vw;
    color: #fff;
  }
  .missionUser {
    width: 10.1667vw;
    height: 1.5625vw * 0.77;
    line-height: 1.5625vw * 0.77;
    font-weight: 500;
    font-size: 1.0417vw;
  }
}
.missionRemark {
  font-weight: 400;
  font-size: 1.0417vw;
  color: #666666;
  margin: 1.0417vw * 0.77 0 0.5208vw * 0.77;
}
.farmStockList {
  width: 70vw;
  display: flex;
}
.farmsStock {
  width: 18.75vw;
  height: 3.2813vw * 0.77;
  margin-right: 1.0417vw;
  background: #e4e7e9;
  border-radius: 12px 12px 12px 12px;
  display: flex;
  line-height: 3.2813vw * 0.77;
  font-size: 0.9375vw;
  .farmsStockIcon {
    width: 1.6667vw;
    height: 1.6667vw;
    margin: auto 1.0417vw;
  }
  .farmsStockName {
    margin: 0 1.5625vw 0 0;
    color: #647f93;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
  }
  .farmsStockNum {
    color: #647f93;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
  }
}
.missionImg {
  margin-top: 0.5208vw * 0.77;
  width: 5.5vw;
  height: 5.5vw;
  background: #dcdedf;
  border-radius: 12px 12px 12px 12px;
  display: inline-block;
  margin-right: 0.8208vw;
  img {
    width: 5.5vw;
    height: 5.5vw;
  }
}

}
</style>
